#include "FiveWin.ch"
#include "TcBrowse.ch"

#define BAR_STANDARD     1
#define BAR_ADDITIONAL   2
#define BAR_DATABASE     3
#define BAR_DIALOG       4
#define BAR_VBX          5

#ifdef __XPP__
   #define Super ::TWindow
#endif

memvar oIDE

//----------------------------------------------------------------------------//

CLASS TToolBar FROM TWindow

   DATA   oTabs
   DATA   oBtnSel

   DATA   aVBX  INIT {}   // Installed VBX info

   CLASSDATA lRegistered AS LOGICAL

   METHOD New() CONSTRUCTOR

   METHOD SelButton( oBtn )
   METHOD SetBar( nBar )

   // Changes the look and action of a ToolBar button
   METHOD SetButton( nBtn, cRes1, cRes2, cTooltip, cCursor, cAction )

   METHOD AddBitmap( nRow, nCol )
   METHOD AddBrowse( nRow, nCol )
   METHOD AddButton( nRow, nCol )
   METHOD AddCheck( nRow, nCol )
   METHOD AddCombo( nRow, nCol )
   METHOD AddDbf( nRow, nCol )
   METHOD AddFolder( nRow, nCol )
   METHOD AddGet( nRow, nCol )
   METHOD AddGroup( nRow, nCol )
   METHOD AddHScroll( nRow, nCol )
   METHOD AddLabel( nRow, nCol )
   METHOD AddListBox( nRow, nCol )
   METHOD AddMeter( nRow, nCol )
   METHOD AddMGet( nRow, nCol )
   METHOD AddPdMenu( nRow, nCol )
   METHOD AddRadio( nRow, nCol )
   METHOD AddReport( nRow, nCol )
   METHOD AddTabs( nRow, nCol )
   METHOD AddTimer( nRow, nCol )
   METHOD AddVbx( nRow, nCol )
   METHOD AddVScroll( nRow, nCol )

   METHOD BuildControl( oBtn, cCursor, cAction )

   METHOD ClearBar( nStart )    // Clear the buttons contents

   METHOD GetVarName( oControl, cInitName )

   METHOD RestoreStatus( oControl )
   METHOD VbxInstall( nPos )

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New() CLASS TToolBar

   local oIni, n := 2, cVbx

   #ifdef __XPP__
      DEFAULT ::lRegistered := .f.

      Super:Init( 375, 210, 451, 628, "Tools",,,,,,,,"N/W",,,"NONE",;
                 ,,.t.,.f., .t. )
   #else
      Super:New( 375, 210, 451, 628, "Tools",,,,,,,,"N/W",,,"NONE",;
                 ,,.t.,.f., .t. )
   #endif

   ::ToolWindow()

   @ 35, 0 TABS ::oTabs PROMPTS "&Standard", "&Additional",;
      "&DataBase", "D&ialogs", "&VBXs", "Win95", "Internet" ;
      SIZE 450, 20 PIXEL OF Self ;
      ACTION ::SetBar( nOption )

   DEFINE BUTTONBAR ::oBar OF Self _3D SIZE 33, 33

   DEFINE BUTTON OF ::oBar ;
      RESOURCE "Arrow", "Arrow2" PRESSED ;
      TOOLTIP  "Pointer" ;
      ACTION   ::SelButton( ::oBar:aControls[ 1 ] ),;
               oIDE:oProject:ResetCursors(),;
               oIDE:oProject:ResetClick()

   ::SelButton( ::oBar:aControls[ 1 ] )

   DEFINE BUTTON OF ::oBar GROUP NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER
   DEFINE BUTTON OF ::oBar NOBORDER

   ::SetBar( BAR_STANDARD )
   ::Show()

   // We don't let this window be destroyed. We just hide it!
   ::bValid = { || ::Hide(), .f. }

   INI oIni FILENAME ".\Visual.ini"
      GET cVbx SECTION "VBX" ENTRY "1" OF oIni
      while ! Empty( cVbx )
         AAdd( ::aVbx, cVbx )
         GET cVbx SECTION "VBX" ENTRY Str( n, If( n < 10, 1, 2 ) ) ;
            OF oIni
         n++
      end
   ENDINI

return Self

//----------------------------------------------------------------------------//

METHOD SelButton( oBtn ) CLASS TToolBar

   if ::oBtnSel == nil
      ::oBtnSel = oBtn
   else
      if ! ( ::oBtnSel == oBtn )
         ::oBtnSel:GoUp()
         ::oBtnSel = oBtn
         ::oBtnSel:Toggle()
      endif
   endif

return nil

//----------------------------------------------------------------------------//

METHOD SetBar( nBar ) CLASS TToolBar

   local n

   ::SelButton( ::oBar:aControls[ 1 ] )
   oIDE:oProject:ResetCursor()
   oIDE:oProject:ResetClick()

   do case
      case nBar == BAR_STANDARD
           ::SetButton( 2, "Static", "Static2", "Label", "PutSay",;
                        "AddLabel()" )
           ::SetButton( 3, "Btn", "Btn2", "Button", "PutBtn",;
                        "AddButton()" )
           ::SetButton( 4, "Get", "Get2", "Get", "PutGet", "AddGet()" )
           ::SetButton( 5, "MGet", "MGet2", "Memo", "PutMGet",;
                        "AddMGet()" )
           ::SetButton( 6, "Check", "Check2", "CheckBox", "PutCheck",;
                        "AddCheck()" )
           ::SetButton( 7, "Radio", "Radio2", "Radios", "PutRad",;
                        "AddRadio()" )
           ::SetButton( 8, "ListBox", "ListBox2", "ListBox", "PutLbx",;
                        "AddListBox()" )
           ::SetButton( 9, "ComboBox", "ComboBo2", "ComboBox", "PutCbx",;
                        "AddCombo()" )
           ::SetButton( 10, "HScroll", "HScroll2", "Hor.Scroll", "PutHScr",;
                        "AddHScroll()" )
           ::SetButton( 11, "VScroll", "VScroll2", "Ver.Scroll", "PutVScr",;
                        "AddVScroll()" )
           ::SetButton( 12, "Group", "Group2", "Group", "PutGrp",;
                        "AddGroup()" )

      case nBar == BAR_ADDITIONAL
           ::SetButton( 2, "Menu", "Menu2", "PullDown Menu",;
                        "PutPdM", "AddPdMenu()" )
           ::SetButton( 3, "Bitmap", "Bitmap2", "Bitmap", "PutBmp",;
                        "AddBitmap()" )
           ::SetButton( 4, "Tabs", "Tabs2", "Tabs", "PutTabs",;
                        "AddTabs()" )
           ::SetButton( 5, "Folder", "Folder2", "Folder", "PutFold",;
                        "AddFolder()" )
           ::SetButton( 6, "Meter", "Meter2", "Meter", "PutMet",;
                        "AddMeter()" )
           ::SetButton( 7, "Timer", "Timer2", "Timer", "PutTim",;
                        "AddTimer()" )
           ::ClearBar( 8 )

      case nBar == BAR_DATABASE
           ::SetButton( 2, "DataBase", "DataBas2", "DataBase",;
                        "PutDbf", "AddDbf()" )
           ::SetButton( 3, "BrowCol", "BrowCol2", "Browse", "PutBrw",;
                        "AddBrowse()" )
           ::SetButton( 4, "RepComp", "RepComp2", "Report", "PutBrw",;
                        "AddReport()" )
           ::SetButton( 5, "Query", "Query2", "Query", "PutBrw",;
                        "AddQuery()" )
           ::SetButton( 6, "Process", "Process2", "Process", "PutBrw",;
                        "AddProcess()" )
           ::ClearBar( 7 )

      case nBar == BAR_DIALOG
           ::ClearBar( 2 )

      case nBar == BAR_VBX
           ::ClearBar( Len( ::aVbx ) + 1 )
           for n = Len( ::aVbx ) + 1 to 11
              ::oBar:aControls[ n ]:cTooltip = "VBX Install"
              ::oBar:aControls[ n ]:bAction  = LocalBlock( Self, n )
           next

   endcase

return nil

//----------------------------------------------------------------------------//

static function LocalBlock( Self, n )

return { || ::VbxInstall( n ) }

//----------------------------------------------------------------------------//

METHOD AddBitmap( nRow, nCol ) CLASS TToolBar

   local oBmp

   @ nRow, nCol BITMAP oBmp SIZE 100, 100 OF oIDE:oActiveForm PIXEL DESIGN

   oBmp:cVarName  = ::GetVarName( oBmp, "oBmp" )
   ::RestoreStatus( oBmp )

return nil

//----------------------------------------------------------------------------//

METHOD AddBrowse( nRow, nCol ) CLASS TToolBar

   local oBrw

   @ nRow, nCol BROWSE oBrw SIZE 200, 150 DESIGN PIXEL OF oIDE:oActiveForm

   oBrw:AddColumn( TCColumn():New( "One",,,,,, 90 ) )
   oBrw:AddColumn( TCColumn():New( "Two",,,,,, 90 ) )
   oBrw:AddColumn( TCColumn():New( "Three",,,,,, 90 ) )

   oBrw:cVarName  = ::GetVarName( oBrw, "oBrw" )
   ::RestoreStatus( oBrw )

return nil

//----------------------------------------------------------------------------//

METHOD AddButton( nRow, nCol ) CLASS TToolBar

   local oBtn, cMethodName
   local nEvent

   @ nRow, nCol BUTTON oBtn PROMPT "&Button" ;
      SIZE 80, 25 OF oIDE:oActiveForm PIXEL DESIGN

   oBtn:cVarName = ::GetVarName( oBtn, "oBtn" )
   cMethodName = SubStr( oBtn:cVarName, 2 ) + "Click"
   nEvent = AScan( oBtn:aEvents, { | aEvent | aEvent[ 1 ] == "OnClick" } )
   oBtn:bLDblClick = { || OSend( oBtn, "_OnClick", cMethodName ),;
                          oIDE:oWndCode:EditMethod( cMethodName,;
                          oIDE:oActiveForm:cClassName,;
                          oBtn:aEvents[ nEvent ] ), 0 }
   ::RestoreStatus( oBtn )

return nil

//----------------------------------------------------------------------------//

METHOD AddCheck( nRow, nCol ) CLASS TToolBar

   local oChk, lValue := .f.
   local cMethodName, nEvent

   @ nRow, nCol CHECKBOX oChk VAR lValue ;
      SIZE 90, 25 OF oIDE:oActiveForm PIXEL DESIGN

   oChk:Set3DLook()
   oChk:cVarName  = ::GetVarName( oChk, "oChk" )
   cMethodName = SubStr( oChk:cVarName, 2 ) + "Click"
   nEvent = AScan( oChk:aEvents, { | aEvent | aEvent[ 1 ] == "OnClick" } )
   oChk:bLDblClick = { || OSend( oChk, "_OnClick", cMethodName ),;
                          oIDE:oWndCode:EditMethod( cMethodName,;
                          oIDE:oActiveForm:cClassName,;
                          oChk:aEvents[ nEvent ] ), 0 }
   ::RestoreStatus( oChk )

return nil

//----------------------------------------------------------------------------//

METHOD AddCombo( nRow, nCol ) CLASS TToolBar

   local oCbx, cValue := "&One"

   @ nRow, nCol COMBOBOX oCbx VAR cValue ;
      ITEMS { "One", "Two", "Three" } ;
      SIZE 85, 60 OF oIDE:oActiveForm PIXEL DESIGN

   oCbx:Set3DLook()
   oCbx:cVarName  = ::GetVarName( oCbx, "oCbx" )
   ::RestoreStatus( oCbx )

return nil

//----------------------------------------------------------------------------//

METHOD AddDbf( nRow, nCol ) CLASS TToolBar

   local oDbf := TDbfComp():New( nRow, nCol, oIDE:oActiveForm, .t.,, "DataBase" )

   oDbf:cVarName    = ::GetVarName( oDbf, "oDbf" )
   ::RestoreStatus( oDbf )

return nil

//----------------------------------------------------------------------------//

METHOD AddFolder( nRow, nCol ) CLASS TToolBar

   local oFld

   @ nRow, nCol FOLDER oFld PROMPTS "&One", "&Two", "T&hree" ;
      SIZE 200, 150 OF oIDE:oActiveForm PIXEL DESIGN

   oFld:cVarName  = ::GetVarName( oFld, "oFld" )
   ::RestoreStatus( oFld )

return nil

//----------------------------------------------------------------------------//

METHOD AddGet( nRow, nCol ) CLASS TToolBar

   local oGet, cText := Space( 20 )

   @ nRow, nCol GET oGet VAR cText ;
      SIZE 120, 25 OF oIDE:oActiveForm PIXEL DESIGN

   oGet:cVarName  = ::GetVarName( oGet, "oGet" )
   oGet:l3D = .t.
   ::RestoreStatus( oGet )

return nil

//----------------------------------------------------------------------------//

METHOD AddGroup( nRow, nCol ) CLASS TToolBar

   local oGrp

   @ nRow, nCol GROUP oGrp ;
      TO 150, 250 PROMPT "&Group" OF oIDE:oActiveForm PIXEL DESIGN

   oGrp:Set3DLook()
   oGrp:cVarName  = ::GetVarName( oGrp, "oGrp" )
   ::RestoreStatus( oGrp )

return nil

//----------------------------------------------------------------------------//

METHOD AddHScroll( nRow, nCol ) CLASS TToolBar

   local oScr, cText := Space( 20 )

   @ nRow, nCol SCROLLBAR oScr HORIZONTAL ;
      SIZE 100, 20 OF oIDE:oActiveForm PIXEL DESIGN

   oScr:cVarName  = ::GetVarName( oScr, "oScr" )
   ::RestoreStatus( oScr )

return nil

//----------------------------------------------------------------------------//

METHOD AddLabel( nRow, nCol ) CLASS TToolBar

   local oSay

   @ nRow, nCol SAY oSay PROMPT "&Label:" ;
      SIZE 45, 20 OF oIDE:oActiveForm PIXEL DESIGN

   oSay:bRClicked = { || MsgInfo( "Click" ) }
   oSay:cVarName  = ::GetVarName( oSay, "oSay" )
   ::RestoreStatus( oSay )

return nil

//----------------------------------------------------------------------------//

METHOD AddListBox( nRow, nCol ) CLASS TToolBar

   local oLbx, cItem

   @ nRow, nCol LISTBOX oLbx VAR cItem ;
      ITEMS { "One", "Two", "Three" } ;
      SIZE 90, 100 OF oIDE:oActiveForm PIXEL DESIGN

   oLbx:cVarName  = ::GetVarName( oLbx, "oLbx" )
   ::RestoreStatus( oLbx )
   oLbx:l3D = .t.

return nil

//----------------------------------------------------------------------------//

METHOD AddMeter( nRow, nCol ) CLASS TToolBar

   local oMet, nValue := 0

   @ nRow, nCol METER oMet VAR nValue ;
      SIZE 110, 20 OF oIDE:oActiveForm PIXEL DESIGN

   oMet:cVarName  = ::GetVarName( oMet, "oMet" )
   ::RestoreStatus( oMet )

return nil

//----------------------------------------------------------------------------//

METHOD AddMGet( nRow, nCol ) CLASS TToolBar

   local oGet, cText := Space( 20 )

   @ nRow, nCol GET oGet VAR cText MEMO HSCROLL ;
      SIZE 200, 150 OF oIDE:oActiveForm PIXEL DESIGN

   oGet:cVarName  = ::GetVarName( oGet, "oMemo" )
   ::RestoreStatus( oGet )
   oGet:l3D = .t.

return nil

//----------------------------------------------------------------------------//

METHOD AddPdMenu( nRow, nCol ) CLASS TToolBar

   local oMnu := TMnuComp():New( nRow, nCol, oIDE:oActiveForm, .t.,, "Menu" )

   oMnu:cVarName    = ::GetVarName( oMnu, "oMenu" )
   ::RestoreStatus( oMnu )
   oIDE:oActiveForm:bMenuSelect = ;
   { | oItem | If( oItem != nil, oIDE:oActiveForm:oWndInsp:SetObject( oItem ),) }
   oIDE:oWndCode:AddData( oMnu:cVarName )

return nil

//----------------------------------------------------------------------------//

METHOD AddRadio( nRow, nCol ) CLASS TToolBar

   local oRad, nValue := 1
   local n

   @ nRow, nCol RADIO oRad VAR nValue ;
      ITEMS "&One", "&Two", "T&hree" ;
      SIZE 90, 25 OF oIDE:oActiveForm PIXEL DESIGN

   SysRefresh()

   oRad:cVarName = ::GetVarName( oRad, "oRadMnu" )
   for n = 1 to 3
      oRad:aItems[ n ]:cVarName = ::GetVarName( oRad:aItems[ n ], "oRad" )
      oRad:aItems[ n ]:bGotFocus = LocalRad( oIDE:oActiveForm, oRad, n )
   next
   oRad:Set3DLook()
   ::RestoreStatus( oRad )

return nil

//----------------------------------------------------------------------------//

METHOD AddReport( nRow, nCol ) CLASS TToolBar

   local oRep := TRepComp():New( nRow, nCol, oIDE:oActiveForm, .t.,, "RepComp" )

   oRep:cVarName    = ::GetVarName( oRep, "oRpt" )
   ::RestoreStatus( oRep )

return nil

//----------------------------------------------------------------------------//

static function LocalRad( oActiveForm, oRad, n )

return { || oActiveForm:oWndInsp:SetObject( oRad:aItems[ n ] ) }

//----------------------------------------------------------------------------//

METHOD AddTabs( nRow, nCol ) CLASS TToolBar

   local oTabs

   @ nRow, nCol TABS oTabs PROMPT "&One", "&Two", "T&hree" ;
      SIZE 200, 20 OF oIDE:oActiveForm PIXEL DESIGN

   oTabs:cVarName  = ::GetVarName( oTabs, "oTab" )
   ::RestoreStatus( oTabs )

return nil

//----------------------------------------------------------------------------//

METHOD AddTimer( nRow, nCol ) CLASS TToolBar

   local oTmr, oTimer

   DEFINE TIMER oTimer INTERVAL 700 OF oIDE:oActiveForm

   oTmr = TComponent():New( nRow, nCol, oIDE:oActiveForm, .t., oTimer, "Timer" )

   oTmr:aProperties = { "cVarName", "lActive", "nInterval", "Cargo" }
   oTmr:aEvents     = { "OnTimer" }
   oTmr:cVarName    = ::GetVarName( oTmr, "oTmr" )
   ::RestoreStatus( oTmr )

return nil

//----------------------------------------------------------------------------//

METHOD AddVbx( nRow, nCol, nBtnPos ) CLASS TToolBar

   local oVbx
   local oBtn := ::oBar:aControls[ nBtnPos ]

   @ nRow, nCol VBX oVbx SIZE 100, 100 OF oIDE:oActiveForm ;
      FILENAME oBtn:Cargo[ 1 ] ;
      CLASS oBtn:Cargo[ 2 ] PIXEL DESIGN

   oVbx:cVarName  = ::GetVarName( oVbx, "oVbx" )
   ::RestoreStatus( oVbx )

return nil

//----------------------------------------------------------------------------//

METHOD AddVScroll( nRow, nCol ) CLASS TToolBar

   local oScr

   @ nRow, nCol SCROLLBAR oScr VERTICAL ;
      SIZE 20, 100 OF oIDE:oActiveForm PIXEL DESIGN

   oScr:cVarName  = ::GetVarName( oScr, "oScr" )
   ::RestoreStatus( oScr )

return nil

//----------------------------------------------------------------------------//

METHOD VbxInstall( nPos ) CLASS TToolBar

   local oBtn := ::oBar:aControls[ nPos ]
   local cVbx := cGetFile( "VBX control(*.vbx)|*.vbx",;
                           "Select a VBX file to install",,;
                           "c:\windows\system" )
   local hVbx

   if Empty( cVbx )
      return nil
   endif

   if File( cVbx )
      hVbx = LoadLibrary( cVbx )
      oBtn:hBitmap1 = LoadBitmap( hVbx, 8000 )
      oBtn:hBitmap2 = LoadBitmap( hVbx, 8001 )
      oBtn:cTooltip = cFileNoExt( cFileNoPath( cVbx ) )
      oBtn:Cargo    = { cVbx, VbxClassName( LoadVbx( hVbx ) ) }
      oBtn:bAction  = ;
               { || ::SelButton( ::oBar:aControls[ nPos ] ),;
                 oIDE:oActiveForm:SetCursor( "PutDbf" ),;
                 oIDE:oActiveForm:bLClicked := { | nRow, nCol | ;
                 ::AddVbx( nRow, nCol, nPos ) } }
      oBtn:Refresh()
      FreeLibrary( hVbx )
   endif

return nil

//----------------------------------------------------------------------------//

METHOD SetButton( nBtn, cRes1, cRes2, cTooltip, cCursor, cAction ) CLASS TToolBar

   local oBtn := ::oBar:aControls[ nBtn ]

   oBtn:LoadBitmaps( cRes1, cRes2 )
   oBtn:cToolTip = cTooltip
   oBtn:bAction  = { || ::BuildControl( oBtn, cCursor, cAction ) }

   oBtn:Refresh()

return nil

//----------------------------------------------------------------------------//

METHOD BuildControl( oBtn, cCursor, cAction ) CLASS TToolBar

   local nForm, n

   ::SelButton( oBtn )

   #ifndef __XPP__
      ASend( oIDE:oProject:aForms, "_SetCursor", cCursor )
   #else
      ASend( oIDE:oProject:aForms, "_SetCursor()", cCursor )
   #endif

   #ifndef __XPP__
      cAction = StrTran( cAction, "()", "" )
   #endif

   ASend( oIDE:oProject:aForms, "_bLClicked",;
           { | nRow, nCol | OSend( Self, cAction,;
           nRow, nCol ), ASend( oIDE:oActiveForm:aControls, "_oCursor", nil ) } )

   for nForm = 1 to Len( oIDE:oProject:aForms )
      for n = 1 to Len( oIDE:oProject:aForms[ nForm ]:aControls )
         oIDE:oProject:aForms[ nForm ]:aControls[ n ]:oCursor = ;
            oIDE:oProject:aForms[ nForm ]:oCursor
         oIDE:oProject:aForms[ nForm ]:aControls[ n ]:bLClicked = ;
            oIDE:oProject:aForms[ nForm ]:bLClicked
      next
   next

return nil

//----------------------------------------------------------------------------//

METHOD ClearBar( nStart ) CLASS TToolBar

   local n, oBtn

   for n = nStart to Len( ::oBar:aControls )
      oBtn = ::oBar:aControls[ n ]
      oBtn:hBitmap1 = 0
      oBtn:hBitmap2 = 0
      oBtn:Refresh()
      oBtn:cToolTip = nil
      oBtn:bAction  = nil
   next

return nil

//----------------------------------------------------------------------------//

METHOD GetVarName( oControl, cInitName ) CLASS TToolBar

   local n, nIndex := 0

   for n = 1 to Len( oIDE:oActiveForm:aControls )
      if oIDE:oActiveForm:aControls[ n ]:ClassName() == oControl:ClassName()
         nIndex++
      endif
   next

return cInitName + Str( nIndex, If( nIndex < 10, 1, 2 ) )

//----------------------------------------------------------------------------//

static function MenuInspect( oMenu )

   local oDlg, oLbx

   DEFINE DIALOG oDlg RESOURCE "MenuInspect"

   REDEFINE LISTBOX oLbx FIELDS "" ;
      HEADERS "MenuItems" ID 110 OF oDlg

   ACTIVATE DIALOG oDlg CENTERED

return nil

//----------------------------------------------------------------------------//

METHOD RestoreStatus( oControl ) CLASS TToolBar

   oControl:bGotFocus = { || oIDE:oInspector:SetObject( oControl ) }
   oControl:bMoved    = { || oIDE:oInspector:SetObject( oControl ) }
   oControl:bResized  = { || oIDE:oInspector:SetObject( oControl ) }
   oControl:SetFocus()
   oIDE:oActiveForm:bLClicked = nil
   ::SelButton( ::oBar:aControls[ 1 ] )
   oIDE:oProject:ResetCursor()
   oIDE:oActiveForm:Add( oControl )
   oIDE:oWndCode:AddData( oControl:cVarName )

return nil

//----------------------------------------------------------------------------//